package com.fenghongzhang.yuekao;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends Fragment {

    private ListView fgLv;
    private Button fgPause;
    private Button fgRestart;
    private Button fgNext;
    private Button fgPre;
    private MyService musicService;
    private Button fgModel;
    public MusicFragment() {
        // Required empty public constructor
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //读取sd
            readMusic();
        }
    }

    private void readMusic() {
        List<Song> music = MusicUtil.getMusic(getActivity());
        FgMusicAdapter fgMusicAdapter = new FgMusicAdapter(getActivity(), music);
        fgLv.setAdapter(fgMusicAdapter);

        //启动服务
        Intent intent = new Intent(getActivity(), MyService.class);
        getActivity().startService(intent);

        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyService.MyBinder binder = (MyService.MyBinder) service;
                musicService = binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        getActivity().bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_music, container, false);


        fgLv = (ListView) inflate.findViewById(R.id.fg_lv);
        fgPause = (Button) inflate.findViewById(R.id.fg_pause);
        fgRestart = (Button) inflate.findViewById(R.id.fg_restart);
        fgNext = (Button) inflate.findViewById(R.id.fg_next);
        fgPre = (Button) inflate.findViewById(R.id.fg_pre);
        fgModel = (Button) inflate.findViewById(R.id.fg_model);


        //数据源 ,sd.
////
//        List<Song> list = new ArrayList<>();
//        list.add(new Song("1","1","1","1","1","1"));
        //sd


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);
        }else{
            readMusic();
        }


        fgLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                musicService.play(position);
            }
        });

        return inflate;
    }
}
